package com.shushan.ui;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.shushan.util.ToolUtils;

public class StrangeWordsRecyclerDivider extends RecyclerView.ItemDecoration {

    private int DP16,DP9,DP12;
    private int colNumPerLine = 5;

    public StrangeWordsRecyclerDivider(){}

    public StrangeWordsRecyclerDivider(Context context, int colNumPerLine) {
        DP16 = ToolUtils.dip2px(context,16f);
        DP9 = ToolUtils.dip2px(context,9f);
        DP12 = ToolUtils.dip2px(context,12f);
        this.colNumPerLine = colNumPerLine;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            if(i/colNumPerLine == 0){//第一行
                if(i%colNumPerLine == (colNumPerLine-1)){//最后一列
                    outRect.set(DP9,DP16,DP12,DP9);
                }else if(i%colNumPerLine == 0){//第一列
                    outRect.set(DP12,DP16,0,DP9);
                }else{//中间列
                    outRect.set(DP9,DP16,0,DP9);
                }
            }else if((i/colNumPerLine) == (childCount/colNumPerLine)){//最后一行
                if(i%colNumPerLine == (colNumPerLine-1)){//最后一列
                    outRect.set(DP9,0,DP12,DP16);
                }else if(i%colNumPerLine == 0){//第一列
                    outRect.set(DP12,0,0,DP9);
                }else{//中间列
                    outRect.set(DP9,0,0,DP9);
                }
            }else {//中间行
                if(i%colNumPerLine == (colNumPerLine-1)){//最后一列
                    outRect.set(DP9,0,DP12,DP9);
                }else if(i%colNumPerLine == 0){//第一列
                    outRect.set(DP12,0,0,DP16);
                }else{//中间列
                    outRect.set(DP9,0,0,DP16);
                }
            }

        }

    }

}